import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * @description：TODO
 * @Author MRyan
 * @Date 2020/11/9 21:06
 * @Version 1.0
 */
public class Customer2 {
    public static void main(String[] args) throws IOException {
        //获取连接对象
        Connection connection = RabbitMqUtil.getConnection("192.168.43.78", 5672, "/ems", "ems", "123");
        //获取通道对象
        Channel channel = connection.createChannel();
        //声明交换机以及交换机类型topic
        channel.exchangeDeclare("topics", "topic");
        //创建临时队列
        String queue = channel.queueDeclare().getQueue();
        //绑定队列和交换机 动态统配符形式route key
        //* 匹配不多不少恰好一个词
        //# 匹配一个或多个词
        //比如 user.# 可以匹配user.save.delete或者user.save等
        //比如user.* 只能匹配user.save
        channel.queueBind(queue, "topics", "user.#");
        //消费消息
        channel.basicConsume(queue, true, new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("消费者2：" + new String(body));
            }
        });

    }
}
